<?php

class Product_push {
    /*
     * 数据表名
     */
    const TABLE_NAME = 'Product_push';
    /*
     * redis命名空间
     */
    const REDIS_NAME = 'Product_push';

    /**
     * 缓存实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_cacheInstance;
    /**
     * redis实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_redisInstance;
    /**
     * db实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_dbInstance;
    /**
     * 单例对象
     */
    public static $singletonObjects;

    /**
     * 获取单例
     */
    public static function getInstance() {
        $className = __CLASS__;
        if (!isset(self::$singletonObjects[$className])) {
            self::$singletonObjects[$className] = new self();
        }

        return self::$singletonObjects[$className];
    }

    /**
     * 构造器
     *
     * @param int $uId
     */
//    protected function __construct() {
//
////        $this->_cacheInstance = $this->_getCache();
//        $this->_dbInstance = $this->_getMysql();
//        $this->_redisInstance = $this->_getRedis();
//    }

    /*
     * 获取cache实例
     */

    public function _getCache() {

        return Common::getCache();
    }

    /*
     * 获取redis实例
     */

    public function _getRedis() {
//        $config = array ('current'=>'127.0.0.1' , 'old' =>'6380') ;
//        $redis = new Helper_Redis($config);
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6380);
        return $redis;
    }

    /*
     * 获取db实例
     */

    public function _getMysql() {

        return Common::getMysql();
    }

    /*
     * 推送产品
     * redisvalue
     */

    public function getPushInfo( $redisValue ) {
       
       if( empty($redisValue )){
          $productInfo = Product_Info::getInstance()->getAllProductInfo();
          $array = array();
          foreach( $productInfo as $key => $value){
            $array[ $value['PId']]= array(
            'productName' => $value['PName'],
            'BCat' => Category_SHow::getInstance()->getCat( $value['BCat'] ),
            'SCat' => Category_SHow::getInstance()->getCat( $value['SCat'] ),
            'Brand'=> Brand_Search::getInstance()->getBrand( $value['PBrand'] ),
            'Price'=> $value['Price']
               );   
        }
         return  $array;
       
       }
       else{
            $field = 'DISTINCT p1.PId';
            $table = 'product_levels p1,product_levels p2,product_levels p3';
            $table1 = 'product_age p1,product_age p2,product_age p3';
            $table2 = 'product_sex p1,product_sex p2';

            $wherelevels = 'p1.PId = p2.PId and p2.PId = p3.PId ';
            $whereage = 'p1.PId = p2.PId and p2.PId = p3.PId  ';
            $wheresex = 'p1.PId = p2.PId ';

        if ( array_key_exists('DHihgLevels', $redisValue) ) {
            $wherelevels .= 'and p1.DId = 5  ';
        }
        if ( array_key_exists('DMiddleLevels', $redisValue) ) {
            $wherelevels .= 'and p2.DId = 4 ';
        }
        if ( array_key_exists('DLowLevels', $redisValue) ) {
            $wherelevels .= 'and p3.DId = 3 ';
        }

        if ( array_key_exists('DOldAge', $redisValue) ) {
            $whereage .= 'and p1.DId = 6 ';
        }
        if ( array_key_exists('DYouthAge', $redisValue) ) {
            $whereage .= 'and p2.DId = 7 ';
        }
        if ( array_key_exists('DInfancyAge', $redisValue) ) {
            $whereage .= 'and p3.DId = 8 ';
        }

        if ( array_key_exists('DManSex', $redisValue) ) {
            $wheresex .= 'and p1.DId = 1 ';
        }
        if ( array_key_exists('DWomanSex', $redisValue) ) {
            $wheresex .= 'and p2.DId = 2';
        }

        $levelsId = Product_Levels::getInstance()->getLevelsProduct($field, $table, $wherelevels);
        $ageId = Product_Age::getInstance()->getAgeProduct($field, $table1, $whereage);
        $sexId = Product_Sex::getInstance()->getSexProduct($field, $table2, $wheresex);
        
        if ($levelsId) {
            $levels = array();
            foreach ($levelsId as $value) {
                $levels[]=$value['PId'];
            }
          
        }
        if ($ageId) {
            $age = array();
            foreach ($ageId as $value) {
                $age[]=$value['PId'];
            }
        }
        if ($sexId) {
            $sex = array();
            foreach ($sexId as $value) {
                $sex[]=$value['PId'];
            }
        }
       
        
        foreach ($redisValue as $key => $value) {    
           //获得产品品牌PId
            if ( $key == 'PBrand' ) {
                $productBrand = Product_Info::getInstance()->getProductInfoByBrand($value);
                if (isset($productBrand)) {
                    $Brand = array();
                    foreach ($productBrand as $value) {
                        $Brand[] = $value['PId'];
                    }
                }
            }
             // 获得所大类对应的PId
            elseif ($key == 'BCat') {
                $productBCat = Product_Info::getInstance()->getProductInfoByCat($value);
                if (isset($productBCat)) {
                    $BCat = array();
                    foreach ($productBCat as $value) {
                        $BCat[] = $value['PId'];
                    }
                }
            }
            //获得所有小类对应PId
            elseif ($key == 'SCat') {
                $productSCat = Product_Info::getInstance()->getProductInfoByCat($value);
                if (isset($productSCat)) {
                    $SCat = array();
                    foreach ($productSCat as $value) {
                        $SCat[] = $value['PId']; 
                    }
                }
            }
            
             //获得所有产品PId
            if ( !array_key_exists( 'PBrand' , $redisValue) ) {
             $productBrand = Product_Info::getInstance()->getAllProductInfo( );
             if (isset($productBrand)) {
                 $Brand = array();
                    foreach ($productBrand as $value) {
                        $Brand[] = $value['PId'];
                    }
                }
             }
             if( !array_key_exists( 'BCat' , $redisValue )){
                 $productBCat = Product_Info::getInstance()->getAllProductInfo( );
                 if (isset($productBCat)) {
                    $BCat = array();
                    foreach ($productBCat as $value) {
                        $BCat[] = $value['PId'];
                    }
                  }
                }
              if( !array_key_exists( 'SCat' , $redisValue ) ){
                  $productSCat = Product_Info::getInstance()->getAllProductInfo( );
                  if (isset($productSCat)) {
                    $SCat = array();
                    foreach ($productSCat as $value) {
                        $SCat[] = $value['PId']; 
                    }
                }
              }  
        }
     
        $array = array_intersect(array_intersect($Brand, $BCat), $SCat);
        //维度
        $arr = array_intersect(array_intersect($levels, $age), $sex);

        $resultPId = array_intersect($array, $arr);
        if( $resultPId ){
            $productSearchInfo = Product_Info::getInstance()->getProductMultiInfoByPId( $resultPId );
         
         $array = array();
         foreach( $productSearchInfo as $key => $value){
           
            $array[$value['PId']]= array(
            'productName' => $value['PName'],
            'BCat' => Category_SHow::getInstance()->getCat( $value['BCat'] ),
            'SCat' => Category_SHow::getInstance()->getCat( $value['SCat'] ),
            'Brand'=> Brand_Search::getInstance()->getBrand( $value['PBrand'] ),
            'Price'=> $value['Price']
               );   
           }
        
            return $array;
        }
        else{
            return '对不起，没有符合的产品';
            
        }
         
           
       }
       
 
    }

    public function __destruct() {
        
    }

}

?>